package com.cmsky.open.food.service.impl;

import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.cmsky.open.base.model.Menu;
import com.cmsky.open.base.service.MenuService;
import com.cmsky.open.cms.enums.CmsNodePropertyKey;
import com.cmsky.open.cms.enums.CmsNodeType;
import com.cmsky.open.cms.model.CmsNodeInfo;
import com.cmsky.open.cms.service.CmsNodeService;

@Service("menuService")
public class FoodMenuService implements MenuService{
	private static final Integer menuNodeId = 6;
	private static final Integer goodsClassfiyNodeId = 17;
	
	private CmsNodeService cmsNodeService;
	
	public void setCmsNodeService(CmsNodeService cmsNodeService) {
		this.cmsNodeService = cmsNodeService;
	}

	public Menu getNavigationMenu() {
		return this.getMenu(menuNodeId);
	}
	
	protected Menu getMenu(Integer nodeId){
		CmsNodeInfo cmsNodeInfo = this.cmsNodeService.findById(nodeId);
		Menu menu = this.parseNodeToMenu(cmsNodeInfo);
		menu.setItem(this.getItems(nodeId));
		return menu;
	}
	
	protected Menu parseNodeToMenu(CmsNodeInfo node){
		Menu item = new Menu();
		CmsNodeType type = CmsNodeType.getByValue(node.getType());
		if(CmsNodeType.Node == type){
			item.setItem(this.getItems(node.getId()));
		}else{
			item.setHref(node.getProperties().get(CmsNodePropertyKey.Href));
		}
		if(node.getProperties().containsKey(CmsNodePropertyKey.Title)){
			item.setName(node.getProperties().get(CmsNodePropertyKey.Title));
		}else{
			item.setName(node.getName());
		}
		return item;
	}
	
	protected List<Menu> getItems(Integer nodeId){
		List<Menu> menuList = new LinkedList<Menu>();
		List<CmsNodeInfo> lists = this.cmsNodeService.findByPid(nodeId);
		if(null!=lists){
			for(CmsNodeInfo node : lists){
				menuList.add(parseNodeToMenu(node));
			}
		}
		return menuList;
	}

	public Menu getGoodsClssfiyMenu() {
		return this.getMenu(goodsClassfiyNodeId);
	}
	

}
